package com.jfinal.weixin.sdk.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.weixin.model.Weather;
import com.jfinal.weixin.model.WeixinInfo;
import com.jfinal.weixin.sdk.msg.out.News;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by 446705260@qq.com on 16/5/4.
 * chengzhichao
 */
public class WeatherApi {


    public static String getWeatherJson(String city, String key) {
        //hongan  CN101200502   b8e3685e50e14cb1a5d2b5b5404a639c
        //xunwu   CN101240716
        //http://www.heweather.com/documents/cn-city-list
        String httpUrl = "https://api.heweather.com/x3/weather?city="+city+"&key="+key;
        String result = request(httpUrl);
        return result;
    }


    public static String request(String httpUrl) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(httpUrl);
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getWeather(){
        WeixinInfo weixinInfo = WeixinInfo.dao.getWeixinInfoById(1);
        String jsonResult = getWeatherJson(weixinInfo.getStr("cityname"),"b8e3685e50e14cb1a5d2b5b5404a639c");

        JSONObject jobj_all =  JSONObject.parseObject(jsonResult);

        JSONArray jary_daily_forecast = jobj_all.getJSONArray("HeWeather data service 3.0").getJSONObject(0).getJSONArray("daily_forecast");
        String min = jary_daily_forecast.getJSONObject(0).getJSONObject("tmp").get("min").toString();//今日最高气温
        String max = jary_daily_forecast.getJSONObject(0).getJSONObject("tmp").get("max").toString();//今日最低气温
        String day = jary_daily_forecast.getJSONObject(0).getJSONObject("cond").get("txt_d").toString();//白天天气
        String night = jary_daily_forecast.getJSONObject(0).getJSONObject("cond").get("txt_n").toString();//白天天气
        String fengxiang = jary_daily_forecast.getJSONObject(0).getJSONObject("wind").get("dir").toString();//风向
        String fengli = jary_daily_forecast.getJSONObject(0).getJSONObject("wind").get("sc").toString();//风力
        String hum = jary_daily_forecast.getJSONObject(0).getString("hum")+"%";

        JSONObject suggestion = jobj_all.getJSONArray("HeWeather data service 3.0").getJSONObject(0).getJSONObject("suggestion");
        String comf =  suggestion.getJSONObject("comf").getString("txt");//舒适度
        String uv =  suggestion.getJSONObject("uv").getString("brf");//紫外线强弱
        String drsg =  suggestion.getJSONObject("drsg").getString("txt");//穿衣建议

        JSONObject now = jobj_all.getJSONArray("HeWeather data service 3.0").getJSONObject(0).getJSONObject("now");
        String cond =  now.getJSONObject("cond").getString("txt");//当前天气情况
        String tmp =  now.getString("tmp");//当前温度

        Weather weather  = new Weather();
        weather.set("title","今日天气奉上").set("temp",min+"至"+max+"度,"+day+"转"+night);
        weather.set("cur",tmp+"度,"+cond).set("light",uv);
        weather.set("cloud",fengxiang+fengli+"级,湿度"+hum).set("light",uv);
        weather.set("suggest",drsg).set("weixinid",1).set("desc","广告文字").set("url","http://www.baidu.com");
        weather.save();
        return weather.toJson();
    }
}
